package com.oauth.google.server.controller;

import java.util.List;

import org.gwtwidgets.server.spring.GWTSpringController;
import org.springframework.beans.factory.annotation.Autowired;

import com.oauth.common.server.util.OAuthRequestUtil;
import com.oauth.google.client.beans.docs.GoogleDocModel;
import com.oauth.google.client.beans.docs.GoogleDocument;
import com.oauth.google.client.beans.orkut.HttpQueryString;
import com.oauth.google.client.exception.GoogleOAuthException;
import com.oauth.google.client.rpc.docs.DocsRemoteService;
import com.oauth.google.server.service.DocsOAuthService;
import com.oauth.logging.OAuthLogger;

public class GoogleDocController extends GWTSpringController implements DocsRemoteService {

	@Autowired
	private DocsOAuthService docsOAuthService;

	private static final long serialVersionUID = -5758464341967618224L;

	private OAuthLogger logger = OAuthLogger.getLogger(this.getClass());

	/**
	 * @param pDocsOAuthService
	 *            the docsOAuthService to set
	 */
	public void setDocsOAuthService(DocsOAuthService pDocsOAuthService) {
		docsOAuthService = pDocsOAuthService;
	}

	@Override
	public GoogleDocModel getGoogleDocuments(HttpQueryString pQueryString)
			throws GoogleOAuthException {
		GoogleDocModel model = new GoogleDocModel();
		Long memberId = OAuthRequestUtil.getMemberId(getThreadLocalRequest());
		List<GoogleDocument> documents;
		try {
			documents = docsOAuthService.getDocuments(memberId);
			model.setDocuments(documents);
		} catch (Exception e) {
			logger.error("Error in getting google docs for member=" + memberId, e);
			throw new GoogleOAuthException();
		}

		return model;
	}

}
